﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace USERS.CYT369.COM.Models
{
    public class LoginHelper
    {
        public static string MakeTGT(USERS.CYT369.COM.MODELS.SysUser user)
        {
            DateTime now = DateTime.Now;
            var random = new Random(now.Second);
            var args = string.Format($"{user.UniqueCode}_{now.Ticks}_{random.Next(1000, 9999)}");
            return LoginHelper.MD5Hash(args);
        }

        public static string MD5Hash(string str)
        {
            if (str != null)
            {
                var hash = System.Security.Cryptography.MD5.Create();
                var buffer = System.Text.Encoding.UTF8.GetBytes(str);
                var hashResult = hash.ComputeHash(buffer);

                var sb = new System.Text.StringBuilder();
                foreach (var num in hashResult)
                {
                    sb.AppendFormat("{0:X2}", num);
                }

                return sb.ToString();
            }

            return string.Empty;
        }

        public static string MakeCaptchaCode()
        {
            var random = new Random(DateTime.Now.Second);
            return random.Next(100000, 999999).ToString();
        }

        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool SendSMS(string phoneNumber, string msg)
        {
            try
            {
                System.IO.File.WriteAllText($"c:\\temp\\{phoneNumber}.log", msg);

                return true;

            }
            catch (Exception ex)
            {
                //todo: 日志处理
                return false;
            }
        }

        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="phoneNumber"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool SendCaptcha(string phoneNumber, string code)
        {
            try
            {
                var msg = $"验证码：{code},有效时间10分钟";

                return SendSMS(phoneNumber, msg);
            }
            catch (Exception ex)
            {
                //todo: 日志处理
                return false;
            }
        }

        ///// <summary>
        ///// 解析IP地址为省市地址
        ///// </summary>
        ///// <param name="ip"></param>
        ///// <returns></returns>
        //public static string IpLookup_soap(string ip)
        //{
        //    var service = new WWW.Webxml.Com.Cn.IpAddressSearchWebServiceSoapClient();
        //    var result = service.getCountryCityByIp(ip);
        //    if (result != null && result.Length > 0)
        //    {
        //        if (result[0] == ip)
        //        {
        //            if (result.Length >= 2)
        //            {
        //                return result[1];
        //            }
        //        }
        //    }

        //    return string.Empty;
        //}

        public static string IpLookup_ip138(string ip)
        {
            if (!string.IsNullOrEmpty(ip))
            {
                try
                {
                    var webClient = new System.Net.WebClient();
                    webClient.Encoding = System.Text.Encoding.UTF8;
                    var respStr = webClient.DownloadString("https://m.ip138.com/iplookup.asp?ip=" + ip);
                    var s = "ASN归属地</td><td>";
                    var startIndex = respStr.IndexOf(s);
                    if (startIndex > 0)
                    {
                        var endIndex = respStr.IndexOf("</td></tr></tbody>", startIndex);
                        var place = respStr.Substring(startIndex + s.Length, endIndex - startIndex - s.Length);
                        return place.Trim();
                    }
                }
                catch (Exception ex)
                {
                }
            }

            return string.Empty;
        }

        public static string IpLookup_ipchaxun(string ip)
        {
            if (!string.IsNullOrEmpty(ip))
            {
                try
                {
                    var webClient = new System.Net.WebClient();
                    webClient.Encoding = System.Text.Encoding.UTF8;
                    var respStr = webClient.DownloadString("https://ipchaxun.com/" + ip);
                    var s = "归属地：</span><span class=\"value\">";
                    var startIndex = respStr.IndexOf(s);
                    if (startIndex > 0)
                    {
                        var endIndex = respStr.IndexOf("</span>", startIndex);
                        var place = respStr.Substring(startIndex + s.Length, endIndex - startIndex - s.Length);
                        return place.Trim();
                    }
                }
                catch (Exception ex)
                {
                }
            }

            return string.Empty;
        }
    }

}
